package so.shanku.essential.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import aym.util.json.JsonMap;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.ImageLoader;
import so.shanku.essential.R;
import so.shanku.essential.app.MyApplication;
import so.shanku.essential.utils.ImageLoaderBitmapCallBack;
import so.shanku.essential.utils.StringUtil;
import so.shanku.essential.utils.Utils;

/**
 * Created by Jaelyn on 2015/9/6 0006.
 * 推荐的商品适配器
 */
public class RecommandProductAdapter extends MyBaseAdapter {
	private int flag = 0;//用来标记是否显示剩余件数

	public int getFlag() {
		return flag;
	}

	public void setFlag(int flag) {
		this.flag = flag;
	}

	private int type = 0;//标记描述是否显示，默认不显示 1：显示
	/**
	 * 隶属item的位置
	 */
	private int parentPosition = 0;

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getParentPosition() {
		return parentPosition;
	}

	public void setParentPosition(int parentPosition) {
		this.parentPosition = parentPosition;
	}

	public RecommandProductAdapter(Context context) {
		super(context);
	}

	@Override
	public View getView(int i, View view, ViewGroup viewGroup) {
		RecommandGoodsItemsAdapterViewHolder recommandGoodsItemsAdapterViewHolder;
		if (view == null) {
			view = layoutInflater.inflate(R.layout.recommand_goods_item, null);
			recommandGoodsItemsAdapterViewHolder = new RecommandGoodsItemsAdapterViewHolder();
			view.setTag(recommandGoodsItemsAdapterViewHolder);
			ViewUtils.inject(recommandGoodsItemsAdapterViewHolder, view);
		} else {
			recommandGoodsItemsAdapterViewHolder = (RecommandGoodsItemsAdapterViewHolder) view.getTag();
		}
		JsonMap data = (JsonMap) datas.get(i);

		String productPicPath = data.getStringNoNull("ProductPic");
		productPicPath=StringUtil.get_SImage(productPicPath);
		if (MyApplication.getInstance().getBitmapHashMap().containsKey(productPicPath)) {
			recommandGoodsItemsAdapterViewHolder.findproduct_hv_item_iv.setImageBitmap((MyApplication.getInstance().getBitmapHashMap().get(productPicPath)));
		} else {
			ImageLoader.getInstance().displayImage(productPicPath, recommandGoodsItemsAdapterViewHolder.findproduct_hv_item_iv, MyApplication.getInstance().getBrandDefaultOption(), new ImageLoaderBitmapCallBack());
		}
		if (type == 0) {
			recommandGoodsItemsAdapterViewHolder.goods_feature_discribe_tv.setVisibility(View.GONE);
		} else {
			recommandGoodsItemsAdapterViewHolder.goods_feature_discribe_tv.setVisibility(View.VISIBLE);
			recommandGoodsItemsAdapterViewHolder.goods_feature_discribe_tv.setText(data.getStringNoNull("brandName"));
		}
		String marketPice = data.getStringNoNull("marketPice");
		String discountPrice=data.getStringNoNull("Price");
		recommandGoodsItemsAdapterViewHolder.goods_price_tv.setText(StringUtil.getFormatMoneyWithSign(marketPice));

		if(!TextUtils.isEmpty(discountPrice)&&!discountPrice.equalsIgnoreCase("0")&&!discountPrice.equalsIgnoreCase(marketPice)) {//有打折
			recommandGoodsItemsAdapterViewHolder.goods_price_discount.setVisibility(View.VISIBLE);
			recommandGoodsItemsAdapterViewHolder.goods_price_discount.setText(StringUtil.getFormatMoneyWithSign(discountPrice));
			Utils.addDeleteLine(recommandGoodsItemsAdapterViewHolder.goods_price_tv);
			recommandGoodsItemsAdapterViewHolder.goods_price_discount.setSelected(true);
		} else {
			Utils.crashDeleteLine(recommandGoodsItemsAdapterViewHolder.goods_price_tv);
			recommandGoodsItemsAdapterViewHolder.goods_price_discount.setVisibility(View.GONE);
		}

		String retainNum=data.getStringNoNull("Number");
		if (TextUtils.isEmpty(retainNum)) {
			recommandGoodsItemsAdapterViewHolder.goods_retain_tv.setVisibility(View.GONE);
		} else {
			recommandGoodsItemsAdapterViewHolder.goods_retain_tv.setVisibility(View.VISIBLE);
			recommandGoodsItemsAdapterViewHolder.goods_retain_tv.setText(retainNum);
			MyApplication.getInstance().startAnimation(recommandGoodsItemsAdapterViewHolder.goods_retain_tv);
		}
		recommandGoodsItemsAdapterViewHolder.goods_name_tv.setText(data.getStringNoNull("SalesName"));
		return view;
	}


	private class RecommandGoodsItemsAdapterViewHolder {
		@ViewInject(R.id.findproduct_hv_item_iv)
		public ImageView findproduct_hv_item_iv; //商品icon
		@ViewInject(R.id.goods_retain_tv)
		public TextView goods_retain_tv;  //剩余提醒
		@ViewInject(R.id.goods_name_tv)
		public TextView goods_name_tv;   //商品名字
		@ViewInject(R.id.goods_feature_discribe_tv)
		public TextView goods_feature_discribe_tv; //描述
		@ViewInject(R.id.goods_price_tv)
		public TextView goods_price_tv; //原价
		@ViewInject(R.id.goods_price_discount_tv)
		public TextView goods_price_discount; //折扣价
	}
}
